/* 
 * File:   enemy.cpp
 * Author: brunoslr
 * 
 * Created on 16 de Setembro de 2009, 00:06
 */

#include "Enemy.h"

Enemy::Enemy(GLint tex_enemy) {

    positionx = WIDTH/2;
    positiony = 4.0;
    explosion = false;
    status = true;
    specialMove = false;
    returnToPosition = false;
    radius = ENEMYWIDTH/2;
    expFrame=0;

    for(int i=0; i<EnemyShoots; i++){
       tiro[i]= new Shoot();
     }
     //drawEnemy(tex_enemy, white);
}
Enemy::Enemy(double x, double y) {
    positionx = x;
    positiony = y;
    status = false;
    explosion = false;
    specialMove = false;
    returnToPosition = false;
    specialMovex = 0;
    specialMovey = 0;
    spDestinationx = 0;
    spDestinationy = 0;
    radius = ENEMYWIDTH/2;
    expFrame=0;
    value = 0;

    for(int i=0; i<EnemyShoots; i++){
       this->tiro[i]= new Shoot();
      }

 }


Enemy::Enemy(double x, double y, int points) {
    positionx = x;
    positiony = y;
    status = true;
    specialMove = false;
    returnToPosition = false;
    specialMovex = 0;
    specialMovey = 0;
    spDestinationx = 0;
    spDestinationy = 0;
    explosion = false;
    radius = ENEMYWIDTH/2;
    expFrame = 0;
    value = points;

    for(int i=0; i<EnemyShoots; i++){
       this->tiro[i]= new Shoot();
      }

 }

Enemy::Enemy(const Enemy& orig) {
}

Enemy::~Enemy() {
}

double Enemy::getPositionx(){
    return positionx;
}

double Enemy::getPositiony(){
    return positiony;
}

bool Enemy::getStatus(){
    return this->status;
}

int Enemy::drawEnemy(GLuint tex_enemy, const double *color ){
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_ALPHA_TEST);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
    glBindTexture(GL_TEXTURE_2D, tex_enemy);
    glBegin(GL_QUADS);
    glColor3f(color[0],color[1],color[2]);
    if(specialMove ||returnToPosition){
        glTexCoord2d(0.0, 1.0);
        glVertex2f(specialMovex - ENEMYWIDTH/2, specialMovey - ENEMYHEIGHT/2);
        glTexCoord2d(0.0, 0.0);
        glVertex2f(specialMovex - ENEMYWIDTH/2, specialMovey + ENEMYHEIGHT/2);
        glTexCoord2d(1.0, 0.0);
        glVertex2f(specialMovex + ENEMYWIDTH/2, specialMovey + ENEMYHEIGHT/2);
        glTexCoord2d(1.0, 1.0);
        glVertex2f(specialMovex + ENEMYWIDTH/2, specialMovey - ENEMYHEIGHT/2);
    }
    else{
        glTexCoord2d(0.0, 1.0);
        glVertex2f(positionx - ENEMYWIDTH/2, positiony - ENEMYHEIGHT/2);
        glTexCoord2d(0.0, 0.0);
        glVertex2f(positionx - ENEMYWIDTH/2, positiony + ENEMYHEIGHT/2);
        glTexCoord2d(1.0, 0.0);
        glVertex2f(positionx + ENEMYWIDTH/2, positiony + ENEMYHEIGHT/2);
        glTexCoord2d(1.0, 1.0);
        glVertex2f(positionx + ENEMYWIDTH/2, positiony - ENEMYHEIGHT/2);
    }
    glEnd();
    glDisable(GL_TEXTURE_2D);
    glDisable(GL_BLEND);
    glClear(GL_COLOR);
    return 0;
}


void Enemy::drawEnemyShoots(){

    for(int i=0; i<EnemyShoots; i++){
       if(this->tiro[i]->status == true){
           this->tiro[i]->drawShoot(ENEMYWIDTH,1);
        }
    }
}


//define o movimento normal do invasor
int Enemy::move(double velocidade) {

   
    positionx+= velocidade;

    return 0;
}

int Enemy::moveShoots(){
 
    for(int i=0; i<EnemyShoots; i++){
        if(this->tiro[i]->status == true){
            tiro[i]->move(-1);
        }
    }
    return 0;
}



int Enemy::TryToShoot(){
    for(int i=0; i<EnemyShoots; i++){
       if(this->tiro[i]->status == false && specialMove==false){
           this->tiro[i] = new Shoot(this->getPositionx(), this->getPositiony(), ENEMYWIDTH/2, 1);
           return 0;
       }
       if(this->tiro[i]->status == false && specialMove){
           this->tiro[i] = new Shoot(this->specialMovex, this->specialMovey, ENEMYWIDTH/2, 1);
           return 0;
       }
   } return 0;
}

void Enemy::deleteShoots(){
       for(int i=0; i<EnemyShoots; i++){
           delete this->tiro[i];
       }

}


int Enemy::HasShoots(){
    for(int i=0; i<EnemyShoots; i++){
       if(this->tiro[i]->status == true){
          return 1;
       }
    } return 0;
}


int Enemy::TrySpecialMove(double targetx){
    if(this->status && this->specialMove == false){
        this->specialMove = true;

        double aux = (positionx + 2*(targetx - positionx)); //auxiliar verifica se a coordenada x do primeiro ponto da trajetoria
                                                            //especial esta dentro da tela
        if(aux > 0.98*WIDTH ||aux < 0.02*WIDTH){
            aux = targetx;
        }
        this->spDestinationx = aux;
        this->spDestinationy = (positiony - BOTTOM)/2;
        this->specialMovex = positionx;
        this->specialMovey = positiony;
    }
}

int Enemy::ReturnToPosition(double velocidade){

    if(specialMove) return 0;

    specialMovex= positionx;
    if(specialMovey+velocidade<=positiony){

        specialMovey=positiony;
        returnToPosition=false;
        return 0;
    }

    specialMovey-= velocidade*2;



    return 0;
}


int Enemy::SpecialMove( double velocidadex, double velocidadey, double coordx){

    if(this->status && this->specialMove == true){
        if(specialMovex > 0.98*WIDTH){
            specialMovex= 0.02*WIDTH;
        }
        if(specialMovex < 0.02*WIDTH){
            specialMovex= 0.98*WIDTH;
        }

        if(specialMovey < BOTTOM){
            specialMovey= 0.98*HEIGHT;
            specialMovex = positionx;
            this->returnToPosition=true;
            this->specialMove=false;
            return 0;
        }
        if (specialMovey>=spDestinationy*1.6){//faz um caminho ate metade da tela

            specialMovex+= (spDestinationx-positionx)*velocidadex;//Primeiro vai para a direcao do ponto dado numa velocidade constante

            }else
                specialMovex+= (spDestinationx-specialMovex)*velocidadex;// depois vai na direcao do ponto de forma suave

            if (specialMovey<=spDestinationy){ // por fim move na direcao do jogador
                spDestinationx = spDestinationx + (coordx - spDestinationx);

            }
            specialMovey-= velocidadey;
 
            return 0;
        }
}

int Enemy::CollisionTestShoots(double x, double y){
  for(int i=0; i<EnemyShoots; i++){
       if(this->tiro[i]->status == true){
            if(this->tiro[i]->collisionTest(LARGURAJOGADOR/2, x,y)){
                this->tiro[i]->status=false;
                return 1;
            }
        }
    }return 0;
}

int Enemy::destroyed(){

    this->status=false;
    this->explosion=true;
    this->expFrame=0;
}

